<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['exam:setting:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['exam:setting:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="支付开启 1开启" align="center" prop="payOpen">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.payOpen" />
        </template>
      </el-table-column>
      <el-table-column label="轮播图高" align="center" prop="bannerHeight" />
      <el-table-column label="每日分享获得积分上限" align="center" prop="shareupper" />
      <el-table-column label="分享标题" align="center" prop="shareTitle" />
      <el-table-column label="公告是否显示" align="center" prop="isDisplay">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.isDisplay" />
        </template>
      </el-table-column>
      <el-table-column label="公告显示条数" align="center" prop="displayNum" />
      <el-table-column label="高频题消耗积分个数" align="center" prop="useIntegralNum" />
      <el-table-column label="每个题型有多少每题多少分" align="center" prop="franction" />
      <el-table-column label="考试时长单位(分)" align="center" prop="paperTime" />
      <el-table-column label="答对一题奖励积分数量" align="center" prop="rewardIntegral" />
      <el-table-column label="勋章设置" align="center" prop="medal" />
      <el-table-column label="每次分享可得积分" align="center" prop="shareIntegral" />
      <el-table-column label="关于我们" align="center" prop="about" />
      <el-table-column label="官方微信号" align="center" prop="wechatNumber" />
      <el-table-column label="每日答题达标数量" align="center" prop="standard" />
      <el-table-column label="及格分数" align="center" prop="pass" />
      <el-table-column label="良好分数" align="center" prop="good" />
      <el-table-column label="优异分数" align="center" prop="excellent" />
      <el-table-column label="倒计时事件名称" align="center" prop="countdown" />
      <el-table-column
        label="时间点"
        align="center"
        prop="countdowntime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="倒计时显示" align="center" prop="timeDisplay">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.timeDisplay" />
        </template>
      </el-table-column>
      <el-table-column label="学生开启 1开启" align="center" prop="studentOpen">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.studentOpen" />
        </template>
      </el-table-column>
      <el-table-column label="题库体验题数" align="center" prop="freepoolnum" />
      <el-table-column label="知识点体验章数" align="center" prop="freeknowledgenum" />
      <el-table-column label="是否必须完善信息1是" align="center" prop="infoStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.infoStatus" />
        </template>
      </el-table-column>
      <el-table-column label="1开启 2关闭" align="center" prop="ios">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.ios" />
        </template>
      </el-table-column>
      <el-table-column label="客服二维码" align="center" prop="customerService" />
      <el-table-column label="状态（0正常 1关闭）" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['exam:setting:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['exam:setting:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SettingForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { SettingApi, SettingVO } from '@/api/exam/setting'
import SettingForm from './SettingForm.vue'

/** 基础设置 列表 */
defineOptions({ name: 'ExamSetting' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SettingVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SettingApi.getSettingPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SettingApi.deleteSetting(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SettingApi.exportSetting(queryParams)
    download.excel(data, '基础设置.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>